//
//  AdsServer.m
//  CYWFrame
//
//  Created by Macx on 2017/8/3.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AdsServer.h"
#import "UIView+LZCView.h"
#import <AXWebViewController.h>
#import "WebViewController.h"


@implementation AdsServer


+ (void)GETAds:(id)param xrView:(XRCarouselView *)xrView {
    
    UIView *v = [[UIView alloc] init];
    
    UIViewController *currVC = [v topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    if (GetUserDefaule(Token) == NULL) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:currVC.view animated:YES];
        [hud hideAnimated:YES afterDelay:1.0];
        double delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            //执行事件
            [MBProgressHUD hideHUDForView:currVC.view animated:YES];
            [self GETAds:param xrView:xrView];
        });
        return;
        
    }
    
    
    
    NSMutableArray *imgArr = [[NSMutableArray alloc] init];
    
    [[NetworkTool sharedTool] requestWithName:@"获取广告" URLString:APIAds parameters:param method:@"GET" callBack:^(id responseObject) {
		
		NSArray *arr = responseObject[@"data"];
        for (NSDictionary *dic in responseObject[@"data"]) {
            [imgArr addObject:dic[@"photo"]];
        }
        xrView.imageArray = imgArr;
		//首页中间banner因为要跳转到附近商家，所以直接return，不让他触发触摸事件
		if (xrView.tag == 1010) {
			return;
		}
		xrView.imageClickBlock = ^(NSInteger index) {
            if(arr.count > index) {
                NSDictionary *dic = arr[index];
                AXWebViewController *webVC = [[AXWebViewController alloc] initWithAddress:dic[@"link_url"]];
                //WebViewController *webVC = [[WebViewController alloc] init];
                //webVC.urlStr = dic[@"link_url"];
                [currVC.navigationController pushViewController:webVC animated:YES];
            }
		};
    }];
}

@end
